package com.fho4565.commands;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.world.level.Explosion;


public class Explode {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
                Commands.literal("explode").requires(s -> s.hasPermission(2))
                        .then(Commands.argument("pos", BlockPosArgument.blockPos())
                                .then(Commands.argument("strength", IntegerArgumentType.integer())
                                        .executes(doExplode(Explosion.BlockInteraction.BREAK))
                                        .then(Commands.literal("destroy").executes(doExplode(Explosion.BlockInteraction.DESTROY)))
                                        .then(Commands.literal("break").executes(doExplode(Explosion.BlockInteraction.BREAK)))
                                        .then(Commands.literal("none").executes(doExplode(Explosion.BlockInteraction.NONE)))
                                )));
    }

    private static Command<CommandSourceStack> doExplode(Explosion.BlockInteraction destroy) {
        return context -> {
            context.getSource().getLevel().explode(
                    null,
                    BlockPosArgument.getLoadedBlockPos(context, "pos").getX(),
                    BlockPosArgument.getLoadedBlockPos(context, "pos").getY(),
                    BlockPosArgument.getLoadedBlockPos(context, "pos").getZ(),
                    IntegerArgumentType.getInteger(context, "strength"),
                    destroy);
            return 1;
        };
    }

}
